﻿/*

Copyright (c) 2009, Stephen Remde
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY STEPHEN REMDE ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL STEPHEN REMDE BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WikiMiner;
using System.Threading;
using System.IO;
using System.Diagnostics;


namespace WindowsFormsApplication1
{
    public partial class SearchForm : Form
    {
        public SearchForm()
        {
            InitializeComponent();
            trackBar1.Value = 79;
            trackBar2.Value = 69;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label5.Text = "Relatedness to Add Node: " + trackBar1.Value + " %";
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            label6.Text = "Relatedness to Add Link: " + trackBar2.Value + " %";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new SearchVisualisation(wikiMinerSearch1.Article, wikiMinerSearch2.Article, trackBar1.Value / 100f, trackBar2.Value / 100f).Visible = true;
        }

        private void button2_Click(object sender, EventArgs ee)
        {
            MakeMovie(wikiMinerSearch1.Article, wikiMinerSearch2.Article);
        }

        private void MakeMovie(Article aa, Article ab)
        {


            SearchContext sc = new SearchContext(aa, ab, trackBar1.Value / 100f, trackBar2.Value / 100f);

            sc.ColorFromStart = true;
            sc.ColorCurrentPath = true;


            VideoUtils v = new VideoUtils();

            Bitmap b = v.CreateCompatibleBitmap();

            Graphics g = Graphics.FromImage(b);
            

            string html = string.Format(
@"<html><style>
body{{font-family:arial;padding:20px}}
h1,h2{{text-align:center}}
h1{{color:lightblue}}
h2{{color:salmon}}
.desc{{display:block;text-align:justify;width:960;padding:20px}}
body,html{{margin:0px;overflow:hidden;}}
</style>
                <body>
<h1>Concept Linkage in Knowledge Repositories</h1>
<h2>Stephen Remde, Peter Cowling - JISCRI Funded Project</h2>
<p><br></p><p><br></p>

<h1>An example A* search linking two concepts</h1>
<div align=center><p class=desc>This video shows a sample A* search linking '{0}' and '{1}'. The search starts at '{0}' and trys to find a path to '{1}' by following links. 
Nodes represents pages on Wikipedia and the arcs between them represent links</p></div>
<p><br></p><p><br></p>

<h2>{0}</h2>
<div align=center><p class=desc>{2}</p></div>

<h2>{1}</h2>
<div align=center><p class=desc>{3}</p></div>"
                , aa.title, ab.title, Sumarize(aa.description), Sumarize(ab.description));




            string html_end =
@"<html><style>
body{{font-family:arial;padding:20px}}
h1,h2{{text-align:center}}
h1{{color:lightblue}}
h2{{color:salmon}}
.desc{{display:block;text-align:justify;width:960;padding:20px}}
body,html{{margin:0px;overflow:hidden;}}
</style>
                <body>
<h1>Concept Linkage in Knowledge Repositories</h1>
<h2>Stephen Remde, Peter Cowling - JISCRI Funded Project</h2>
<p><br></p><p><br></p>
<p><br></p><p><br></p>
<p><br></p><p><br></p>
<p><br></p><p><br></p>
<h1>More Information</h1>
<div align=center><p class=desc>For more information follow the links in the video's description or go to the following site:</p></div>
<div align=center><p class=desc>http://conceptlinkage.blogspot.com/</p></div>

";


            v.Fade(html, 0, 25 * 10, 25 * 2);
          
          
            do
            {
                for (int i = 0; i < 50; i++)
                {
                    sc.Graph.Organize(4);
                    sc.Graph.AutoAdjustViewport(b.Width, b.Height, 0.80f,
                       sc.Graph.GetNodes().Where(n => sc.Path != null && sc.Path.Any(a => a.title == n.text)));
                    sc.Graph.Draw(g, true);

                    v.AddFrame(b);                    
                }
            } while (!sc.Step());

            for (int i = 0; i < 5 * 50; i++)
            {
                sc.Graph.Organize(4);
                sc.Graph.AutoAdjustViewport(b.Width, b.Height, 0.80f,
                   sc.Graph.GetNodes().Where(n => sc.Path != null && sc.Path.Any(a => a.title == n.text)));
                sc.Graph.Draw(g, true);

                v.AddFrame(b);
            }

            for (int i = 0; i < 25 * 20; i++)
            {
                sc.Graph.Organize(4);
                sc.Graph.AutoAdjustViewport(b.Width, b.Height, 0.99f);
                sc.Graph.Draw(g, true);

                v.AddFrame(b);
            }

            v.Fade(html_end, 25, 25 * 10, 0);

            v.Encode("c:\\movies\\" + makesafepath(aa) + " - " + makesafepath(ab) + ".flv");
        }

        private object Sumarize(string p)
        {
            int i = p.IndexOf('.');
            if (i > -1)
            {
                p = p.Substring(0, i+1);
            }
            return p;
        }

        private string makesafepath(Article article)
        {
            char[] disallowed = Path.GetInvalidFileNameChars();

            var ret = article.title.ToCharArray();

            for (int i = 0; i < ret.Length; i++)
                if (disallowed.Contains(ret[i]))
                    ret[i] = '_';

            return new string(ret);

        }
    }
}